<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="客户名称" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入客户名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="客户编码" prop="customerCode">
        <el-input v-model="queryParams.customerCode" placeholder="请输入客户编码" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <!-- <el-form-item label="日期">
        <el-date-picker v-model="daterangeDate" style="width: 240px" value-format="yyyy-MM-dd" type="daterange"
          range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </el-form-item> -->
      <el-form-item label="税务登记证号码" prop="vat">
        <el-input v-model="queryParams.vat" placeholder="请输入税务登记证号码" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <!-- <el-form-item label="官网地址" prop="website">
        <el-input v-model="queryParams.website" placeholder="请输入官网地址" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="摘要" prop="comment">
        <el-input v-model="queryParams.comment" placeholder="请输入摘要" clearable @keyup.enter.native="handleQuery" />
      </el-form-item> -->
      <!-- <el-form-item label="信用额度" prop="creditLimit">
        <el-input v-model="queryParams.creditLimit" placeholder="请输入信用额度" clearable @keyup.enter.native="handleQuery" />
      </el-form-item> -->
      <!-- <el-form-item label="Active" prop="active">
        <el-input v-model="queryParams.active" placeholder="请输入Active" clearable @keyup.enter.native="handleQuery" />
      </el-form-item> -->
      <!-- <el-form-item label="是否为客户" prop="customer">
        <el-input v-model="queryParams.customer" placeholder="请输入是否为客户" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="是否为供应商" prop="supplier">
        <el-input v-model="queryParams.supplier" placeholder="请输入是否为供应商" clearable @keyup.enter.native="handleQuery" />
      </el-form-item> -->
      <!-- <el-form-item label="为否为雇员" prop="employee">
        <el-input v-model="queryParams.employee" placeholder="请输入为否为雇员" clearable @keyup.enter.native="handleQuery" />
      </el-form-item> -->
      <el-form-item label="街道" prop="street">
        <el-input v-model="queryParams.street" placeholder="请输入街道" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="所属区" prop="zip">
        <el-input v-model="queryParams.zip" placeholder="请输入所属区" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="城市" prop="city">
        <el-input v-model="queryParams.city" placeholder="请输入城市" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      
      <!-- <el-form-item label="State" prop="stateId">
        <el-input v-model="queryParams.stateId" placeholder="请输入State" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="国家id" prop="countryId">
        <el-input v-model="queryParams.countryId" placeholder="请输入国家id" clearable @keyup.enter.native="handleQuery" />
      </el-form-item> -->
      <!-- <el-form-item label="电子邮箱" prop="email">
        <el-input v-model="queryParams.email" placeholder="请输入邮箱" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="联系电话" prop="phone">
        <el-input v-model="queryParams.phone" placeholder="请输入联系电话" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="移动电话" prop="mobile">
        <el-input v-model="queryParams.mobile" placeholder="请输入移动电话" clearable @keyup.enter.native="handleQuery" />
      </el-form-item> -->
      <!-- <el-form-item label="公司/个人" prop="isCompany">
        <el-input v-model="queryParams.isCompany" placeholder="请输入公司/个人" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="Share Partner" prop="partnerShare">
        <el-input v-model="queryParams.partnerShare" placeholder="请输入Share Partner" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="Commercial Entity" prop="commercialPartnerId">
        <el-input v-model="queryParams.commercialPartnerId" placeholder="请输入Commercial Entity" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="Company Name Entity" prop="commercialCompanyName">
        <el-input v-model="queryParams.commercialCompanyName" placeholder="请输入Company Name Entity" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="创建者" prop="writeUid">
        <el-select v-model="queryParams.writeUid" placeholder="请选择创建者" clearable>
          <el-option v-for="dict in dict.type.boshien_emp" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="记录日期">
        <el-date-picker v-model="daterangeWriteDate" style="width: 240px" value-format="yyyy-MM-dd" type="daterange"
          range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </el-form-item>
      <el-form-item label="客户编码" prop="customerCode">
        <el-input v-model="queryParams.customerCode" placeholder="请输入客户编码" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="定作方地址" prop="deliveryAddress">
        <el-input v-model="queryParams.deliveryAddress" placeholder="请输入定作方地址" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
         >新增</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['system:partner:edit']">修改</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['system:partner:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
         >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="partnerList" @selection-change="handleSelectionChange" @row-click="getDetail">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="id" align="center" prop="id" /> -->
      <el-table-column label="客户名称" align="center" prop="name" />
      <el-table-column label="客户编码" align="center" prop="customerCode" />
      <el-table-column label="电子邮箱" align="center" prop="email" />
      <el-table-column label="联系电话" align="center" prop="phone" />
      <!-- <el-table-column label="移动电话" align="center" prop="mobile" /> -->
      <!-- <el-table-column label="公司/个人" align="center" prop="isCompany" /> -->
      <el-table-column label="城市" align="center" prop="city" /> 
      <el-table-column label="所属区" align="center" prop="zip" />
      <el-table-column label="街道" align="center" prop="street" />
      <!-- <el-table-column label="展示名称" align="center" prop="displayName" /> -->
      <!-- <el-table-column label="日期" align="center" prop="date" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.date, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column> -->
      <el-table-column label="税务登记证号码" align="center" prop="vat" />
      <!-- <el-table-column label="官网地址" align="center" prop="website" /> -->
      <!-- <el-table-column label="摘要" align="center" prop="comment" /> -->
      <!-- <el-table-column label="信用额度" align="center" prop="creditLimit" /> -->
      <!-- <el-table-column label="Active" align="center" prop="active" /> -->
      <!-- <el-table-column label="是否为客户" align="center" prop="customer" /> -->
      <!-- <el-table-column label="是否为供应商" align="center" prop="supplier" /> -->
      <!-- <el-table-column label="为否为雇员" align="center" prop="employee" /> -->
      <!-- <el-table-column label="Address Type" align="center" prop="type" /> -->

      <!-- <el-table-column label="State" align="center" prop="stateId" /> -->
      <!-- <el-table-column label="国家id" align="center" prop="countryId" /> -->

      <!-- <el-table-column label="Share Partner" align="center" prop="partnerShare" />
      <el-table-column label="Commercial Entity" align="center" prop="commercialPartnerId" />
      <el-table-column label="Company Name Entity" align="center" prop="commercialCompanyName" />
      <el-table-column label="创建者" align="center" prop="writeUid">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.boshien_emp" :value="scope.row.writeUid" />
        </template>
      </el-table-column> -->
      <!-- <el-table-column label="记录日期" align="center" prop="writeDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.writeDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column> -->

      <!-- <el-table-column label="定作方地址" align="center" prop="deliveryAddress" /> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" @click.stop>
        <template slot-scope="scope" >
          <el-button size="mini" type="text" icon="el-icon-document" @click.stop="getDetail(scope.row)"
            >客户详情</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click.stop="handleUpdate(scope.row)"
            >修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click.stop="handleDelete(scope.row)"
            v-hasPermi="['system:partner:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改合作伙伴对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>

      <el-form ref="form" :model="form" :rules="rules" label-width="150px">


        <!-- <el-form-item label="修改人" prop="updateBy">
          <el-input v-model="form.updateBy" placeholder="请输入客户名称" />
        </el-form-item> -->
        <!-- <el-form-item label="修改日期" prop="updateTime">
          <el-input v-model="form.updateTime" placeholder="请输入展示名称" />
        </el-form-item> -->
        <!-- =========== -->
        <el-form-item label="客户名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入客户名称" />
        </el-form-item>
        <el-form-item label="客户编码" prop="customerCode">
          <el-input v-model="form.customerCode" placeholder="请输入客户编码，该编码用于生产销售订单号，通常为客户名称缩写大写字母" />
        </el-form-item>
        <!-- <el-form-item label="展示名称" prop="displayName">
          <el-input v-model="form.displayName" placeholder="请输入展示名称" />
        </el-form-item> -->
        <!-- <el-form-item label="日期" prop="date">
          <el-date-picker clearable v-model="form.date" type="date" value-format="yyyy-MM-dd" placeholder="请选择日期">
          </el-date-picker>
        </el-form-item> -->
        <el-form-item label="税务登记证号码" prop="vat">
          <el-input v-model="form.vat" placeholder="请输入税务登记证号码" />
        </el-form-item>
        <el-form-item label="官网地址" prop="website">
          <el-input v-model="form.website" placeholder="请输入官网地址" />
        </el-form-item>
        <!-- <el-form-item label="摘要" prop="comment">
          <el-input v-model="form.comment" placeholder="请输入摘要" />
        </el-form-item> -->
        <!-- <el-form-item label="信用额度" prop="creditLimit">
          <el-input v-model="form.creditLimit" placeholder="请输入信用额度" />
        </el-form-item> -->
        <!-- <el-form-item label="Active" prop="active">
          <el-input v-model="form.active" placeholder="请输入Active" />
        </el-form-item> -->
        <el-form-item label="是否为客户" prop="customer">
          <el-radio v-model="form.customer" label="t">是</el-radio>
                    <el-radio v-model="form.customer" label="f">否</el-radio>
          <!-- <el-input v-model="form.customer" placeholder="请输入是否为客户" /> -->
        </el-form-item>
       
        <el-form-item label="是否为供应商" prop="supplier">
          <el-radio v-model="form.supplier" label="t">是</el-radio>
                    <el-radio v-model="form.supplier" label="f">否</el-radio>
          <!-- <el-input v-model="form.supplier" placeholder="请输入是否为供应商" /> -->
        </el-form-item>
        <el-form-item label="省/市/区" prop="account">
          <!-- <v-distpicker :province="form.province" :city="form.city" :area="form.zip"></v-distpicker> -->
          <v-distpicker :province="form.province" :area="form.zip" :city="form.city" @province="onChangeProvince"
            @area="onChangeArea" @city="onChangeCity">
          </v-distpicker>
        </el-form-item>

        <!-- <el-form-item label="为否为雇员" prop="employee">
          <el-input v-model="form.employee" placeholder="请输入为否为雇员" />
        </el-form-item> -->
        <el-form-item label="街道" prop="street">
          <el-input v-model="form.street" placeholder="请输入街道" />
        </el-form-item>
        <!-- <el-form-item label="所属区" prop="zip">
          <el-input v-model="form.zip" placeholder="请输入所属区" />
        </el-form-item>
        <el-form-item label="城市" prop="city">
          <el-input v-model="form.city" placeholder="请输入城市" />
        </el-form-item> -->
        <!-- <el-form-item label="State" prop="stateId">
          <el-input v-model="form.stateId" placeholder="请输入State" />
        </el-form-item> -->
        <el-form-item label="客户类型" prop="countryId">
          <el-radio v-model="form.countryId" label="0">国内</el-radio>
                    <el-radio v-model="form.countryId" label="1">国外</el-radio>
          <!-- <el-input v-model="form.countryId" placeholder="请输入国家id" /> -->
        </el-form-item>
        <el-form-item label="电子邮箱" prop="email">
          <el-input v-model="form.email" placeholder="请输入邮箱" />
        </el-form-item>
        <el-form-item label="联系电话" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入联系电话" />
        </el-form-item>
        <el-form-item label="移动电话" prop="mobile">
          <el-input v-model="form.mobile" placeholder="请输入移动电话" />
        </el-form-item>
        <el-form-item label="邮寄地址" prop="deliveryAddress">
          <el-input v-model="form.deliveryAddress" placeholder="例如：山西省太原市杏花岭区小返乡水沟村，张三，18011111111" />
        </el-form-item>
        <!-- 参数没改 -->
        <el-form-item label="发票地址" prop="deliveryAddress">
          <el-input v-model="form.deliveryAddress" placeholder="例如：山西省太原市杏花岭区小返乡水沟村，张三，18011111111" />
        </el-form-item>

        <el-form-item label="银行名称" prop="bankName">
          <el-input v-model="form.bankName" placeholder="请输入银行名称" />
        </el-form-item>
        <el-form-item label="银行识别代码" prop="bankBic">
          <el-input v-model="form.bankBic" placeholder="请输入银行识别代码" />
        </el-form-item>
        <el-form-item label="银行账户号码" prop="accountNumber">
          <el-input v-model="form.accountNumber" placeholder="请输入银行账户号码" />
        </el-form-item>
        <!-- <el-form-item label="公司/个人" prop="isCompany">
          <el-input v-model="form.isCompany" placeholder="请输入公司/个人" />
        </el-form-item> -->
        <!-- <el-form-item label="Share Partner" prop="partnerShare">
          <el-input v-model="form.partnerShare" placeholder="请输入Share Partner" />
        </el-form-item>
        <el-form-item label="Commercial Entity" prop="commercialPartnerId">
          <el-input v-model="form.commercialPartnerId" placeholder="请输入Commercial Entity" />
        </el-form-item>
        <el-form-item label="Company Name Entity" prop="commercialCompanyName">
          <el-input v-model="form.commercialCompanyName" placeholder="请输入Company Name Entity" />
        </el-form-item> -->
        <!-- <el-form-item label="创建者" prop="writeUid">
          <el-select v-model="form.writeUid" placeholder="请选择创建者">
            <el-option v-for="dict in dict.type.boshien_emp" :key="dict.value" :label="dict.label"
              :value="parseInt(dict.value)"></el-option>
          </el-select>
        </el-form-item> -->
        <!-- <el-form-item label="记录日期" prop="writeDate">
          <el-date-picker clearable :default-value="form.writeDate" v-model="form.writeDate" type="date"
            value-format="yyyy-MM-dd" placeholder="请选择记录日期">
          </el-date-picker>
        </el-form-item> -->

        <!-- <el-form-item label="定作方地址" prop="deliveryAddress">
          <el-input v-model="form.deliveryAddress" placeholder="请输入定作方地址" />
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import VDistpicker from 'v-distpicker'
import { listPartner, getPartner, delPartner, addPartner, updatePartner, getdetail, gettext } from "@/api/system/partner";
import partnerdetail from "@/views/system/partnerdetail/index";
export default {
  name: "Partner",
  components: { VDistpicker },
  dicts: ['boshien_emp'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 合作伙伴表格数据
      partnerList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 定作方地址时间范围
      daterangeDate: [],
      // 定作方地址时间范围
      daterangeWriteDate: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        date: null,
        vat: null,
        website: null,
        comment: null,
        creditLimit: null,
        active: null,
        customer: 't',
        supplier: null,
        employee: null,
        type: null,
        street: null,
        zip: null,
        city: null,
        stateId: null,
        countryId: null,
        email: null,
        phone: null,
        mobile: null,
        isCompany: null,
        partnerShare: null,
        commercialPartnerId: null,
        commercialCompanyName: null,
        writeUid: null,
        writeDate: null,
        customerCode: null,
        deliveryAddress: null
      },
      // 表单参数
      form: {
        // 省市区
        province: '',
        city: '',
        area: '',
      },
      // 表单校验
      rules: {
        name: [{ required: true, message: '客户名称不能为空', trigger: 'blur' }],
        customerCode: [{ required: true, message: '客户编码不能为空', trigger: 'blur' }] ,
        countryId: [{ required: true, message: '客户类型不能为空', trigger: 'blur' }] 

      },

    };
  },
  created() {
    this.getList();
    // this.getNow()
  },
  methods: {

    // 省赋值
    onChangeProvince(data) {
      this.form.province = data.value
    },
    // 市赋值
    onChangeCity(data) {
      this.form.city = data.value
    },
    //  区赋值
    onChangeArea(data) {
      this.form.zip = data.value
    },
   
    /** 查询合作伙伴列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeDate && '' != this.daterangeDate) {
        this.queryParams.params["beginDate"] = this.daterangeDate[0];
        this.queryParams.params["endDate"] = this.daterangeDate[1];
      }
      if (null != this.daterangeWriteDate && '' != this.daterangeWriteDate) {
        this.queryParams.params["beginWriteDate"] = this.daterangeWriteDate[0];
        this.queryParams.params["endWriteDate"] = this.daterangeWriteDate[1];
      }
      listPartner(this.queryParams).then(response => {
        // console.log(response);
        this.partnerList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 查询详情按钮
    getDetail(row) {
      const id = row.id
      this.$router.push({
        path: "/partnerdetail",
        component: partnerdetail,
        redirect: "noRedirect",
        hidden: false,
        alwaysShow: true,
        query: { listId: id },
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        displayName: null,
        date: null,
        parentId: null,
        vat: null,
        website: null,
        comment: null,
        creditLimit: null,
        active: null,
        customer: 't',
        supplier: 'f',
        employee: null,
        type: null,
        street: null,
        zip: null,
        city: null,
        stateId: null,
        countryId: null,
        email: null,
        phone: null,
        mobile: null,
        isCompany: null,
        partnerShare: null,
        commercialPartnerId: null,
        commercialCompanyName: null,
        writeUid: null,
        writeDate: null,
        customerCode: null,
        deliveryAddress: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeDate = [];
      this.daterangeWriteDate = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加合作伙伴";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getPartner(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改合作伙伴";
       
      });

    },

    /** 提交按钮 */
    submitForm() {

      // console.log(this.form);
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updatePartner(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPartner(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除名称为"' + row.name + '"的客户？').then(function () {
        return delPartner(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/partner/export', {
        ...this.queryParams
      }, `partner_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
